﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class updatepass : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            string userName = Request.Form["userName"].Trim();
            string oldpass = Request.Form["oldpass"].Trim();
            string newpass = Request.Form["newpass"].Trim();
            string confpass = Request.Form["confpass"].Trim();
            if (string.IsNullOrWhiteSpace(userName))
            {
                Response.Write("<script type='text/javascript'>alert('用户名不能为空!');</script>");
                return;
            }
            if (string.IsNullOrWhiteSpace(oldpass))
            {
                Response.Write("<script type='text/javascript'>alert('旧密码不能为空!');</script>");
                return;
            }
            if (string.IsNullOrWhiteSpace(newpass))
            {
                Response.Write("<script type='text/javascript'>alert('新密码不能为空!');</script>");
                return;
            }
            if (newpass.Length < 6)
            {
                Response.Write("<script type='text/javascript'>alert('新密码长度至少6位!');</script>");
                return;
            }
            if (!newpass.Equals(confpass))
            {
                Response.Write("<script type='text/javascript'>alert('两次输入密码不一致!');</script>");
                return;
            }
            DirectoryInfo TheFolder = new DirectoryInfo(Server.MapPath("userdata"));
            DirectoryInfo[] folders = TheFolder.GetDirectories(userName);
            if (folders.Length == 0)
            {
                Response.Write("<script type='text/javascript'>alert('不存在这个用户名!');</script>");
                return;
            }
            XmlDocument doc = new XmlDocument();
            try
            {
                string userxml = Server.MapPath("userdata/" + userName + "/user.xml");
                doc.Load(userxml);
                string em = doc.SelectSingleNode("/info/em").InnerText;
                string ps = doc.SelectSingleNode("/info/ps").InnerText;
                if (!ps.Equals(oldpass))
                {
                    Response.Write("<script type='text/javascript'>alert('旧密码错误!');</script>");
                    return;
                }
                doc.SelectSingleNode("/info/ps").InnerText = newpass;
                doc.Save(userxml);
                Response.Write("<script type='text/javascript'>alert('修改成功!');</script>");
            }
            catch
            {
                Response.Write("<script type='text/javascript'>alert('读取用户信息失败!');</script>");
                return;
            }

        }
    }

    private bool SendPassEmail(string em, string ps)
    {
       
        return true;
    }
}